﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace DataImporterExporter
{
    public class Serializer<T>
    {

        public void SerializeObject(string fileName, ObjectsToSerialize<T> objectsToSerialize)
        {
            Stream stream = File.Open(fileName, FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(stream, objectsToSerialize);
            
            stream.Close();
        }

        public ObjectsToSerialize<T> DeSerializeObject(string fileName)
        {
            ObjectsToSerialize<T> objectsToSerialize;
            Stream stream = File.Open(fileName, FileMode.Open);
            BinaryFormatter binaryFormatter= new BinaryFormatter();
            object obj = binaryFormatter.Deserialize(stream);
            objectsToSerialize = (ObjectsToSerialize<T>)obj;
            stream.Close();
            return objectsToSerialize;
        }
    }
}
